package com.jims.api.v1.controller.doctor;

import com.baomidou.kisso.my.controller.BaseController;
import com.jims.master.common.data.Message;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.modules.base.netHosptial.service.NetSettingService;
import com.jims.master.modules.base.order.entity.Sorders;
import com.jims.master.modules.base.order.service.SordersService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.patient.myFamily.entity.MyFamily;
import com.jims.master.modules.patient.myFamily.service.MyFamilyService;
import com.jims.master.modules.patient.myFriend.service.SysUserMessageService;
import com.jims.master.modules.sync.entity.ClinicMaster;
import com.jims.master.modules.sys.entity.OrgDict;
import com.jims.master.modules.sys.entity.SysOrgDict;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.DictService;
import com.jims.master.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/16 0016.
 */
@RestController
@RequestMapping(value = "/doctor")
public class DoctorController extends BaseController {
    @Autowired
    private MasterDoctorinfoService masterDoctorinfoService;
    @Autowired
    private MasterBaseinfoService masterBaseinfoService;
    @Autowired
    private DictService dictService;
    @Autowired
    private SordersService sordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private SysUserMessageService sysUserMessageService;
    @Autowired
    private NetSettingService netSettingService;
    @Autowired
    private MyFamilyService myFamilyService;

    /**
      * @Title: doctorList
      * @Description: 根据条件查询医生列表
      * @param titles
      * @param parts
      * @return
      * @author CT
      * @date 2017/11/21 0017
      * @throws
     */
    @RequestMapping(value = "V100/doctorList",method ={RequestMethod.GET,RequestMethod.POST})
    public String doctorListV100(String hospital,String parts,String area,String pageNo,String titles,String name){
	/*	Map<String, Object> map = new HashMap<String, Object>();*/
		/*map.put("registList",registList);*/
        MasterDoctorinfo masterDoctorinfo = new MasterDoctorinfo();
        MasterBaseinfo masterBaseinfo = new MasterBaseinfo();
        masterDoctorinfo.setParts(parts);
        masterDoctorinfo.setTitles(titles);
        masterDoctorinfo.setRegisterPlace(area);
        int pageNum = Integer.parseInt(pageNo)*10;
        masterDoctorinfo.setPageNum(pageNum);
        masterDoctorinfo.setMechanism(hospital);
        masterBaseinfo.setName(name);
        masterDoctorinfo.setBaseinfo(masterBaseinfo);
        List doctorList = masterDoctorinfoService.findListIndex(masterDoctorinfo);
        Message mes = new Message();
        mes.setData(doctorList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title: saveDoctor
     * @Description: 移動端申请成为医生信息
     * @return
     * @author CT
     * @date 2018/02/12 0018
     * @throws
     */

    @RequestMapping(value = "V100/saveDoctor", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDoctor(@RequestParam(required = true) Map map) {
        Message msa = new Message();
        MasterBaseinfo masterBaseinfo = masterBaseinfoService.saveDoctor(map);
        if(masterBaseinfo==null){
            msa.setStatus("0");
            msa.setData("基本信息保存失败");
        }else{
            msa.setData(masterBaseinfo);
        }
        return SecurityUtil.encryptMsgData(msa);
    }

    /**
     * @Title: doctorFlag
     * @Description: 判斷醫生是否审核
     * @return
     * @author CT
     * @date 2018/02/12 0018
     * @throws
     */
    @RequestMapping(value = "V100/doctorFlag", method = {RequestMethod.GET, RequestMethod.POST})
    public String doctorFlag(@RequestParam(required = true) String openid) {
        Message msa = new Message();
        User user =userService.findUserDocOpenId(openid);
        MasterDoctorinfo masterDoctorinfo = masterDoctorinfoService.getDoctorinfoByBaseinfoId(user.getPersionId());
        String  docFlag = null;
        if(masterDoctorinfo!=null){
            docFlag = masterDoctorinfo.getDocFlag();
        }
        msa.setData(docFlag);
        return SecurityUtil.encryptMsgData(msa);
    }



    /**
     * @param userId
     * @return java.lang.String
     * @throws
     * @Title: getDoctorV100
     * @Description: （根据医生id获取医生信息）
     * @author jict
     * @date 2017-11-16
     */
    @RequestMapping(value = "V100/getDoctor", method = {RequestMethod.GET, RequestMethod.POST})
    public String getDoctor(@RequestParam(required = true) String userId) {
        Message msa = new Message();
        MasterBaseinfo masterBaseinfo = masterBaseinfoService.get(userId);
        MasterDoctorinfo masterDoctorinfo = masterDoctorinfoService.getDocByBaseinfo(masterBaseinfo);
        if (masterDoctorinfo!=null) {
            masterBaseinfo.setSex(dictService.getDictName("10021",masterBaseinfo.getSex()));
            masterBaseinfo.setNation(dictService.getDictName("10003", masterBaseinfo.getNation()));
            masterBaseinfo.setMarried(dictService.getDictName("10004", masterBaseinfo.getMarried()));
            masterDoctorinfo.setBaseinfo(masterBaseinfo);
        }
        msa.setData(masterDoctorinfo);
        return SecurityUtil.encryptMsgData(msa);
    }

     /**
       * @Title:updateDoctor
       * @Description: （医生信息维护）
        * @param baseinfo id
        * @param doGoodAt 擅长
        * @param headPath 头像
        * @param profile 简介
       * @return
       * @author jict
       * @date 2017/11/27
       * @throws
      */
    @RequestMapping(value = "V100/updateDoctor",method ={RequestMethod.GET,RequestMethod.POST})
    public String updateDoctor(@RequestParam(required = true)String baseinfo,String doGoodAt,String headPath,String profile ){
        Message mes = new Message();
        MasterBaseinfo masterbaseinfo=masterBaseinfoService.get(baseinfo);
        MasterDoctorinfo masterDoctorinfo =masterDoctorinfoService.getDocByBaseinfo(masterbaseinfo);
        if(doGoodAt!=null&&!"".equals(doGoodAt)){
            masterDoctorinfo.setDoctorGoodAt(doGoodAt);
        }
        if (headPath!=null&&!"".equals(headPath)) {
            masterbaseinfo.setHeadPath(headPath);
        }
        if (profile!=null&&!"".equals(profile)) {
            masterDoctorinfo.setDoctorProfile(profile);
        }
        int result = masterDoctorinfoService.save(masterDoctorinfo);
        int base = masterBaseinfoService.save(masterbaseinfo);
        if (result == 1 || base==1) {
            mes.setData("修改成功");
        }else{
            mes.setData("修改失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }
    /**
     * @Title:updateDoctor
     * @Description: （医生信息维护）
     * @param map
     * @return
     * @author WangYQ
     * @date 2018/04/12
     * @throws
     */
    @RequestMapping(value = "V200/updateDoctor",method ={RequestMethod.GET,RequestMethod.POST})
    public String updateDoctorInfo(@RequestParam(required = true) Map map){
        Message mes = new Message();
        MasterBaseinfo masterBaseinfo=new MasterBaseinfo();
        masterBaseinfo.setId((String) map.get("baseinfo"));
        MasterDoctorinfo masterDoctorinfo =masterDoctorinfoService.getDocByBaseinfo(masterBaseinfo);
        if(masterDoctorinfo!=null){
            //        masterDoctorinfo.setWorkPic((String) map.get("work_pic"));
            masterDoctorinfo.setLicencePic((String) map.get("licence_pic"));
            masterDoctorinfo.setTitlePic((String) map.get("title_pic"));
            int i =masterDoctorinfoService.save(masterDoctorinfo);
            if (i>0) {
                mes.setData("修改成功");
            }else{
                mes.setData("修改失败");
            }
        }else {
            mes.setData("该医生信息不存在");
        }
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:updateDoctor
     * @Description: （获取所有的医院信息）
     * @return
     * @author jict
     * @date 2017/11/27
     * @throws
     */
    @RequestMapping(value = "V100/hospitalList",method ={RequestMethod.GET,RequestMethod.POST})
    public String hospitalList(MasterDoctorinfo masterDoctorinfo){
        Message mes = new Message();
        List<String> result = masterDoctorinfoService.getMechanism();
        mes.setData(result);
        return SecurityUtil.encryptMsgData(mes);
    }



    /**
     * @Title:updateDoctor
     * @Description: （我的患者）
     * @return
     * @author jict
     * @date 2017/2/1
     * @throws
     */
    @RequestMapping(value = "V100/myPatient",method ={RequestMethod.GET,RequestMethod.POST})
    public String myPatient(String doctorId){
        Message mes = new Message();
        List result= new ArrayList<MasterBaseinfo>();
        //已签约的患者
        List<String> listId = sordersService.getPatientId(doctorId);
        for(int i=0;i<listId.size();i++){
            MasterBaseinfo ma = masterBaseinfoService.getInfo(listId.get(i));
            if (ma!=null) {
                result.add(ma);
                ma.setRemarks("1");
            }
        }
        //未签约患者
        MasterDoctorinfo md = masterDoctorinfoService.get(doctorId);
        MasterBaseinfo masterBaseinfo = md.getBaseinfo();
        User user=userService.findUserByPersonId(masterBaseinfo.getId());
        String Id = user.getId();
        List<String> no = sysUserMessageService.getPatient(Id);
        for(int j=0;j<no.size();j++){
            User user1 = userService.get(no.get(j));
            if (user1!=null){
                MasterBaseinfo ma1 = masterBaseinfoService.getInfo(user1.getPersionId());
                ma1.setRemarks("0");
                result.add(ma1);
            }
        }
        mes.setData(result);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:getMyPatient
     * @Description: （获取我的患者）
     * @return
     * @author WangYQ
     * @date 2018/03/19
     * @throws
     */
    @RequestMapping(value = "V100/getMyPatient",method ={RequestMethod.GET,RequestMethod.POST})
    public String getMyPatient(@RequestParam(required = true)String id,int pageSize,int pageNum){
        Message mes = new Message();
        List<MasterBaseinfo> result = masterBaseinfoService.getMyPatient(id,pageSize,pageNum);
        mes.setData(result);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title: getOrganizationV100
     * @Description: （根据parentid查询机构）
     * @param id
     * @return java.lang.String
     * @author WangYQ
     * @date 2018-03-20
     * @throws
     */
    @RequestMapping(value = "V100/getSignedOrgDict",method ={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String getSignedOrgDict(@RequestParam(required = true) String id){
        Message msg=new Message();
        List<OrgDict> list=netSettingService.getSignedOrgDict(id);
        if(list==null){
            msg.setStatus("0");
            msg.setData("该医生信息不全");
        }else if(list.size()==0){
            msg.setStatus("0");
            msg.setData("该医生暂无签约医院");
        }else{
            msg.setData(list);
        }
        return SecurityUtil.encryptMsgData(msg);
    }

    /**
     * @Title:getMyPatient
     * @Description: （添加我的家人）
     * @return
     * @author jict
     * @date 2018/06/21
     * @throws
     */
    @RequestMapping(value = "V100/save",method ={RequestMethod.GET,RequestMethod.POST})
    public String save(MyFamily my){
        Message mes = new Message();
        int relationship = myFamilyService.judgeRelationship(my);
        if(relationship>0){
            mes.setStatus("2");
            mes.setData("亲属关系存在");
        }else {
            int result = myFamilyService.save(my);
            if (result > 0){
                mes.setData("保存成功");
            }else{
                mes.setData("保存失败");
            }
        }
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:getMyPatient
     * @Description: （查找我的家人）
     * @return
     * @author jict
     * @date 2018/06/21
     * @throws
     */
    @RequestMapping(value = "V100/myfamilyList",method ={RequestMethod.GET,RequestMethod.POST})
    public String myfamilyList(String personId,@RequestParam(required = true)String pageNo,String pageSize, HttpServletRequest request, HttpServletResponse response){
        Page<MyFamily> page = new Page<MyFamily>();
        Map<String ,String> map = new HashMap<String, String>();
        map.put("personId",personId);
        map.put("pageNo",pageNo);
        map.put("pageSize",pageSize);
        page = myFamilyService.findFamilyById(new Page<MyFamily>(request, response), map);
        Message mes = new Message();
        List<MyFamily> myFamilyList = page.getList();
        mes.setData(myFamilyList);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title:getMyPatient
     * @Description: （删除我的家人）
     * @return
     * @author jict
     * @date 2018/06/21
     * @throws
     */
    @RequestMapping(value = "V100/delete",method ={RequestMethod.GET,RequestMethod.POST})
    public String delete(String id){
        Message mes = new Message();
        int i  = myFamilyService.delete(id);
        if (i>0) {
            mes.setData("删除成功");
        }else{
            mes.setData("删除失败");
        }
        return SecurityUtil.encryptMsgData(mes);
    }
}
